﻿using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using CommonLib;
using Model;

namespace ProjectStudent
{
    public partial class ClassManager : Form
    {
        public ClassManager()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false; // 禁止自动生成列
            LoadClassPage();
        }
        ClassBLL classBLL = new ClassBLL();
        public void GetAllClass()
        {
            DataTable dt = classBLL.GetClass();
            dataGridView1.DataSource = dt;
        }

        public void LoadClassPage()
        {

            int total = classBLL.GetClassCount("");
            lb_total.Text = total.ToString();
            int pageSize = Convert.ToInt32(comboBox1.Text);
            int totalPage = (int)Math.Ceiling((double)total / pageSize);
            lb_pageCount.Text = totalPage.ToString();
            int pageNumber = Convert.ToInt32(txtCurrentPage.Text);


            DataTable dt = classBLL.GetClassPage(pageNumber, pageSize, "");
            dataGridView1.DataSource = dt;
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            txtCurrentPage.Text = "1";
            LoadClassPage();
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            int pageNumber = Convert.ToInt32(txtCurrentPage.Text);
            if (pageNumber > 1)
            {
                pageNumber--;
                txtCurrentPage.Text = pageNumber.ToString();
            }
            else
            {
                MessageBox.Show("已经是第一页了");
            }
            LoadClassPage();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            int pageNumber = Convert.ToInt32(txtCurrentPage.Text);
            int totalPage = Convert.ToInt32(lb_pageCount.Text);
            if (pageNumber < totalPage)
            {
                pageNumber++;
                txtCurrentPage.Text = pageNumber.ToString();
            }
            else
            {
                MessageBox.Show("已经是最后一页了");
            }
            LoadClassPage();
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            int totalpage = Convert.ToInt32(lb_pageCount.Text);
            txtCurrentPage.Text = totalpage.ToString();
            LoadClassPage();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtCurrentPage.Text = "1";
            LoadClassPage();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show("请选择要删除的行！");
                return;
            }
            DataRow rowView = dataGridView1.CurrentRow.DataBoundItem as DataRow;
            int ClassID = (int)rowView["ClassID"];
            if (MessageBox.Show("确认要删除该用户吗？", "确认删除", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (classBLL.DeleteClass(ClassID))
                {
                    MessageBox.Show("删除成功！");
                    LoadClassPage();
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadClassPage();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string className = Microsoft.VisualBasic.Interaction.InputBox("请输入班级名称", "新增班级");
            string grade = Microsoft.VisualBasic.Interaction.InputBox("请输入年级", "年级");
            string department = Microsoft.VisualBasic.Interaction.InputBox("请输入所属院校", "所属院校");
            string createDate = Microsoft.VisualBasic.Interaction.InputBox("请输入创建时间", "创建时间");
            if (!string.IsNullOrEmpty(className))
            {
                if (classBLL.AddClass(className, Convert.ToInt32(grade), department, Convert.ToDateTime(createDate)))
                {
                    MessageBox.Show("新增成功！");
                    LoadClassPage(); // 刷新数据
                }
                else
                {
                    MessageBox.Show("新增失败！");
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {

        }

        private void btnExport_Click(object sender, EventArgs e)
        {

        }

        private void btnImport_Click(object sender, EventArgs e)
        {

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string keyword = txtSearch.Text;
            if (keyword == null)
            {
                GetAllClass();
                return;
            }
            DataTable dt = classBLL.SearchKey(keyword);
            if (dt.Rows.Count > 0)
            {
                dataGridView1.DataSource = dt;
            }
            else
            {
                dataGridView1.DataSource = null;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            //GetAllClass();

        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                DataRow rowView = dataGridView1.Rows[e.RowIndex].DataBoundItem as DataRow;
                if (rowView != null)
                {
                    int classID = (int)rowView["ClassID"];
                    string columnName = dataGridView1.Columns[e.ColumnIndex].Name;
                    object newValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

                    if (classBLL.UpdateClass(classID, columnName, newValue))
                    {
                        MessageBox.Show("更新成功！");
                    }
                    else
                    {
                        MessageBox.Show("更新失败！");
                    }
                }
            }

        }

        private void log4(object sender, EventArgs e)
        {
            LogHelp.LogInit();
            LogHelp.Info("111111oooooo");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataTable dt = classBLL.GetClass();
            List<Class> listClass = typeConvert.ToDataList<Class>(dt);
            List<Class> ts = listClass.Where(p => p.ClassName == "计算机科学与技术1班").ToList();


        }
    }
}
